#include "PointOnScreen.h"
#include <QPainter>
#include <QDebug>
#include <Windows.h>

PointOnScreen::PointOnScreen(int numberOfPoints)
	: QWidget(nullptr), Filter(numberOfPoints, 0), mutex(QMutex::Recursive)
{
	inBuffer = new QList<QPoint>();
	outBuffer = new QList<QPoint>();
}

PointOnScreen::~PointOnScreen()
{
	delete inBuffer;
	delete outBuffer;
}

void PointOnScreen::Process()
{
	inBuffer->clear();
	for (int i = 0; i < GetInputsCount(); ++i)
	{
		if (*inputValid[i])
		{
			float *ptr = inputs[i]->ptr<float>(0);
			QPoint point(ptr[0], ptr[1]);
			inBuffer->append(point);
		}
	}

	mutex.lock();
	qSwap(inBuffer, outBuffer);
	mutex.unlock();

	this->update();
}

void PointOnScreen::paintEvent(QPaintEvent *event)
{	
	//paint from buffer 1
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	QPainterPath path;
	bool lock = mutex.tryLock();
	if (!lock)
		return;

	for (int i = 0; i < outBuffer->size(); ++i)
	{
		path.addEllipse(outBuffer->at(i), 10, 10);
	}
	mutex.unlock();
	painter.fillPath(path, QColor(0,0,0));
}

QWidget *PointOnScreen::GetOutputWidget()
{
	this->show();
	this->showFullScreen();

	return nullptr;
}
